package com.ssm.service.impl;

import com.ssm.dao.TeacherMapper;
import com.ssm.entity.Teacher;
import com.ssm.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 教师服务实现类
 */
@Service
public class TeacherServiceImpl implements TeacherService {

    @Autowired
    private TeacherMapper teacherMapper;

    @Override
    @Cacheable(value = "teacherList", key = "'all'")
    public List<Teacher> findAll() {
        return teacherMapper.selectAll();
    }

    @Override
    @Cacheable(value = "teacher", key = "#id")
    public Teacher findById(Integer id) {
        return teacherMapper.selectById(id);
    }

    @Override
    @Transactional
    @CacheEvict(value = {"teacherList", "teacher"}, allEntries = true)
    public boolean add(Teacher teacher) {
        return teacherMapper.insert(teacher) > 0;
    }

    @Override
    @Transactional
    @CacheEvict(value = {"teacherList", "teacher"}, allEntries = true)
    public boolean update(Teacher teacher) {
        return teacherMapper.update(teacher) > 0;
    }

    @Override
    @Transactional
    @CacheEvict(value = {"teacherList", "teacher"}, allEntries = true)
    public boolean delete(Integer id) {
        return teacherMapper.deleteById(id) > 0;
    }

    @Override
    @Cacheable(value = "teacherList", key = "'department:' + #department")
    public List<Teacher> findByDepartment(String department) {
        return teacherMapper.selectByDepartment(department);
    }
} 